using System;
using System.Web.UI;
using Eneta.Portal.Forum.Controls.Base;
using Eneta.Portal.Forum.Controls.Common;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;

namespace Eneta.Portal.Forum.Controls
{
    /// <summary>
    /// MessageControl is responsible for displaying all the indvidual
    /// messages (posts) for a given topic.
    /// </summary>
    public class ViewMessages : BaseForumControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewMessages"/> class.
        /// </summary>
        public ViewMessages()
        {
            Load += PostsLoad;
        }

        /// <summary>
        /// Notifies server controls that use composition-based implementation to create any child
        /// controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            var topic = RepositoryRegistry.TopicRepository.GetById(_topicID);

            Controls.Add(BuildBasePageLinks());
            Controls.Add(new LiteralControl("<br>"));

            Controls.Add(new LiteralControl("<table width=100% cellspacing=0 cellpadding=3>"));
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td align=left>&nbsp;</td>"));
            Controls.Add(new LiteralControl("<td align=right>"));

            var forum = RepositoryRegistry.ForumRepository.GetById(_forumID);
            
            if(ForumApplication.Instance.CurrentUser != null)
                if (forum.HasAccess(ForumApplication.Instance.CurrentUser, Permission.Rights.Reply))
                {
                    Controls.Add(new LiteralControl(
                                     String.Format("<a href=\"{0}\">{1}</a>&nbsp;|",
                                                   ForumApplication.Instance.GetReplyLink(_topicID, PostMode.Reply.ToString()),
                                                   Resources.UI.TextReply	
                                         )));
                }

            Controls.Add(new LiteralControl("&nbsp;"));

            if(ForumApplication.Instance.CurrentUser != null)
                if (forum.HasAccess(ForumApplication.Instance.CurrentUser, Permission.Rights.Add))
                {
                    Controls.Add(new LiteralControl(
                                     String.Format("<a href=\"{0}\">{1}</a>&nbsp;|",
                                                   ForumApplication.Instance.GetNewTopicLink(_forumID, PostMode.New.ToString()),
                                                   Resources.UI.TextNewTopic
                                         )));
                }

            Controls.Add(new LiteralControl("&nbsp;"));

            if(ForumApplication.Instance.CurrentUser != null)
                if (ForumApplication.Instance.CurrentUser.IsAdmin)
                {
                    string deleteLink = ForumApplication.Instance.GetLink(SharePointForumControls.DeleteTopic, "topic={0}", topic.Id);
                    Controls.Add(new LiteralControl(
                                     string.Format("<a href=\"{0}\">{1}</a>", deleteLink, Resources.UI.TextDeleteTopic)));
                }

            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            Controls.Add(new LiteralControl("</table>"));

            Controls.Add(new LiteralControl("<table width=100%>"));

            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl(string.Format("<td colspan=2 class=\"ms-ToolPaneTitle\">{0}</td>", topic.Name)));
            Controls.Add(new LiteralControl("</tr>"));

            DisplayMessages(topic.Messages, forum);

            Controls.Add(new LiteralControl("</table>"));
        }
 
        #region Private Methods
        /// <summary>
        /// Handles the Load event of the Posts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PostsLoad(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RepositoryRegistry.TopicRepository.IncreaseViewCount(_topicID);
            }
        }
        #endregion
    }
}